package org.cnc.explain.lifecycle;

import org.cnc.explain.lifecycle.beandefinitionregistry_3.BeanDefinitionRegistryBean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;

import java.util.Arrays;

/**
 * <p>
 * #TODO: desc
 * </p>
 *
 * @author zhiyong.tan
 * @since 2022-08-25
 */
public class Step3BeanDefinitionRegistry {
	@Test
	public void testBeanDefinitionRegistry() {
		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

		// 定义一个 bean definition
		GenericBeanDefinition gbd = new GenericBeanDefinition();
		gbd.setBeanClass(BeanDefinitionRegistryBean.class);

		gbd.setInstanceSupplier(()  -> new BeanDefinitionRegistryBean());
		// 注册
		beanFactory.registerBeanDefinition("bean", gbd);

		System.out.println(beanFactory.getBeanDefinition("bean"));
		System.out.println(beanFactory.containsBeanDefinition("bean"));
		System.out.println(Arrays.asList(beanFactory.getBeanDefinitionNames()));

		/**
		 *  核心重点在 getBean的内部调用方法: {@link org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean}
		 */
		BeanDefinitionRegistryBean bean = beanFactory.getBean(BeanDefinitionRegistryBean.class);

		System.out.println(bean);
	}
}
